package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import model.AbstractModel;
import model.Funcoes;


public class PersistenciaDAO {

	protected Connection connection;
	protected Conexao objConexao;

	public PersistenciaDAO() {
		objConexao = Conexao.getInstance();
		connection = objConexao.abrirConexao();
	}

	public boolean inserir(AbstractModel object) {
		Boolean result = Boolean.FALSE;

		HashMap<String, Object> dados = object.getDados();

		String sql = "Insert into " + object.getTabela() + " ( ";

		Set<String> keys = dados.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String string = (String) it.next();
			sql += string;
			if (it.hasNext()) {
				sql += ",";
			}
		}
		sql += " ) values ( ";
		it = keys.iterator();
		while (it.hasNext()) {
			String string = (String) it.next();
			Object valor = dados.get(string);
			if (valor instanceof Date) {
				valor = Funcoes.dateToSql((Date) valor);
			}
			if (valor instanceof String) {
				valor = "'" + ((String) valor).replaceAll("'", "\\\\'") + "'";
			}
			sql += valor;
			if (it.hasNext()) {
				sql += ",";
			}
		}
		sql += " );";

		PreparedStatement pstmt;
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.execute();
			System.out.println(" Objeto inserido no banco de dados");
			result = Boolean.TRUE;
		} catch (SQLException e) {
			System.out.println("Erro ao inserir");
			e.printStackTrace();
		}
		return result;
	}

	public boolean alterar(AbstractModel object) {
		Boolean result = Boolean.FALSE;
		HashMap<String, Object> dados = object.getDados();

		String sql = "update " + object.getTabela() + " set ";

		Set<String> keys = dados.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String string = (String) it.next();
			Object valor = dados.get(string);
			if (valor instanceof Date) {
				valor = Funcoes.dateToSql((Date) valor);
			}
			if (valor instanceof String) {
				valor = "'" + ((String) valor).replaceAll("'", "\\\\'") + "'";
			}
			sql += string + " = " + valor;
			if (it.hasNext()) {
				sql += ",";
			}
		}
		sql += " where " + object.getCampoId() + " = " + object.getId();

		PreparedStatement pstmt;
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.execute();
			System.out.println("Objeto alterado no banco de dados");
			result = Boolean.TRUE;
		} catch (SQLException e) {
			System.out.println("Erro ao alterar");
			e.printStackTrace();
		}
		return result;
	}

	public boolean delete(AbstractModel object) {
		Boolean result = Boolean.FALSE;
		String sql = "delete from " + object.getTabela() + " where "
				+ object.getCampoId() + " = " + object.getId();

		PreparedStatement pstmt;
		try {
			pstmt = connection.prepareStatement(sql);
			pstmt.execute();
			System.out.println(" Objeto deletado do banco de dados");
			result = Boolean.TRUE;
		} catch (SQLException e) {
			System.out.println("Erro ao deletar");
			e.printStackTrace();
		}
		return result;
	}
}
